package com.apptravel.web.controller.admin;

import com.apptravel.dao.AreaDAO;
import com.apptravel.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-11
 * Time: 17:43:21
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class AdminAreaBaseController extends AdminBaseController{

    @Autowired
    protected AreaDAO areaDao;
    


    /**
	 * �� ������.
	 *
	 * @return
	 */
	/*protected String makeTree(String basePath,boolean isCheck){

		StringBuilder sb = new StringBuilder();
		if(isCheck){
			sb.append("<ul class='tree treeFolder treeCheck expand'>");
		}else{
			sb.append("<ul class='tree treeFolder expand'>");
		}

		sb.append("<li><a href='"+basePath+"0' target='ajax' rel='contentBox'>������</a>");
		sb.append("<ul>");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("parentid", 0L);
		List<Area> parentAreas = areaDao.getListNoPage(params);
		for(Area area:parentAreas){
			makeSubTree(area,sb,basePath,isCheck);
        }
		sb.append("</ul>");
		sb.append("</li>");
		sb.append("</ul>");
		return sb.toString();
	}*/

	/**
	 * �����ӽڵ�.
	 * @param area
	 * @param sb
	 */
	/*private void makeSubTree(Area area,StringBuilder sb,String basePath,boolean isCheck){
		if(haveChilds(area)){
			sb.append("<li>");
			if(isCheck){
				sb.append("<a href='"+basePath+area.getId()+"' tname='channelId' tvalue='"+area.getId()+"'>"+area.getAreaname()+"</a>");
			}else{
				sb.append("<a href='"+basePath+area.getId()+"' target='ajax' rel='contentBox'>"+area.getAreaname()+"</a>");
			}

			sb.append("<ul>");
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("parentid", area.getId());
			List<Area> subs = areaDao.getListNoPage(params);
			for(Area a:subs){
                //makeSubTree(a,sb,basePath,isCheck);
				sb.append("<li>");
					if(isCheck){
						sb.append("<a href='"+basePath+a.getId()+"' tname='channelId' tvalue='"+a.getId()+"'>"+a.getAreaname()+"</a>");
					}else{
						sb.append("<a href='"+basePath+a.getId()+"' target='ajax' rel='contentBox'>"+a.getAreaname()+"</a>");
					}

				sb.append("</li>");
			}
			sb.append("</ul>");
			sb.append("</li>");
		}else{
			sb.append("<li>");
			if(isCheck){
				sb.append("<a href='"+basePath+area.getId()+"' tname='channelId' tvalue='"+area.getId()+"'>"+area.getAreaname()+"</a>");
			}else{
				sb.append("<a href='"+basePath+area.getId()+"' target='ajax' rel='contentBox'>"+area.getAreaname()+"</a>");
			}

			sb.append("</li>");
		}

	}*/

	/**
	 * �ж��Ƿ����ӽڵ�.
	 *
	 * @param area
	 * @return
	 */
	protected boolean haveChilds(Area area){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("parentid", area.getId());
		List<Area> list = areaDao.getListNoPage(params);
		if(list.size() > 0){
			return true;
		}else{
			return false;
		}
	}


    class AreaNode{

        Long id;

        String name;

        boolean isParent;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean getIsParent() {
            return isParent;
        }

        public void setIsParent(boolean parent) {
            isParent = parent;
        }
    }
}
